﻿Imports System.Data.OleDb
Imports BUS
Imports DTO
Public Class frmLapLich

    Dim datasrrc As DataSet
    Dim tuyenBindingSource As New BindingSource
    Dim tuyenLichLapBindingSource As New BindingSource
    Dim tuyenThuLapBindingSource As New BindingSource

    Friend WithEvents tuyenNgayKhongLapBindingSource As New BindingSource
    Dim tuyenThoiDiemKhongLapBindingSource As New BindingSource

    Private Sub LICHLAP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
        BindingData()

    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Try
            If xl_kiemtraThem() = True Then
                Dim row As DataRowView = tuyenLichLapBindingSource.AddNew()
                row.Row.SetField(1, txtGio.Text + ":" + txtPhut.Text)

            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        If (tuyenThuLapBindingSource.Position = -1) Then
            Dim row As DataRowView = tuyenThuLapBindingSource.AddNew()
            row.Row.SetField("ThuHai", cbxThuHai.Checked)
            row.Row.SetField("ThuBa", cbxThuBa.Checked)
            row.Row.SetField("ThuTu", cbxThuTu.Checked)
            row.Row.SetField("ThuNam", cbxThuNam.Checked)
            row.Row.SetField("ThuSau", cbxThuSau.Checked)
            row.Row.SetField("ThuBay", cbxThuBay.Checked)
            row.Row.SetField("ChuNhat", cbxChuNhat.Checked)
        End If

        EndEdit()

        LichTuyenBus.CapNhatThoiDiemKhoiHanh(datasrrc.Tables(1))
        LichTuyenBus.CapNhatThuLap(CType(tuyenThuLapBindingSource.Current, DataRowView).Row)

        datasrrc.Tables(2).AcceptChanges()
        RefreshBindingCheckBox()

    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        CancelEdit()
    End Sub

    Private Sub btnThemThoiDiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemThoiDiem.Click
        Try
            If xl_kiemtraThemKhongLap() = True Then
                If (tuyenNgayKhongLapBindingSource.Position = -1) Then
                    Return
                End If

                Dim row As DataRowView = tuyenThoiDiemKhongLapBindingSource.AddNew()
                row.Row.SetField("ThoiDiemKhoiHanh", txtGioKhongLap.Text + ":" + txtPhutKhongLap.Text)

                Dim dt As DataRowView = tuyenNgayKhongLapBindingSource.Current
                row.Row.SetField("MaTuyen", dt.Row.Field(Of Integer)("MaTuyen"))
                row.Row.SetField("NgayKhoiHanh", dt.Row.Field(Of Date)("NgayKhoiHanh"))
                tuyenThoiDiemKhongLapBindingSource.EndEdit()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnThemNgay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemNgay.Click
        If xl_kiemtraNgayKhoiHanh() = True Then
            Dim row As DataRowView = tuyenNgayKhongLapBindingSource.AddNew()

            row.Row.SetField("NgayKhoiHanh", dtpNgayKhoiHanh.Value)
            tuyenNgayKhongLapBindingSource.EndEdit()
            datasrrc.Tables(3).AcceptChanges()
        End If
    End Sub

    Private Sub btnCapNhatThoiDiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatThoiDiem.Click
        EndEdit()
        LichTuyenBus.CapNhatThoiDiemKhoiHanhKhongLap(datasrrc.Tables(4))
    End Sub

    Private Sub dgvDanhSachLichLap_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDanhSachLichLap.CellContentClick
        If (dgvDanhSachLichLap.Columns(e.ColumnIndex).Name = "colXoa") Then
            tuyenLichLapBindingSource.RemoveAt(e.RowIndex)
        End If
    End Sub

    Private Sub NgayKhoiHanh_PositionChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles tuyenNgayKhongLapBindingSource.CurrentItemChanged
        If (tuyenNgayKhongLapBindingSource.Position = -1) Then
            Return
        End If

        Dim rowView As DataRowView = tuyenNgayKhongLapBindingSource.Current
        If (rowView.Row.ItemArray.Length > 3) Then
            Return
        End If

        Dim filter As String = String.Format("NgayKhoiHanh = #{1:MM/dd/yyyy}# AND MaTuyen= {0}", _
                                                                rowView.Row.Field(Of Integer)("MaTuyen"), _
                                                              rowView.Row.Field(Of Date)("NgayKhoiHanh"))
        tuyenThoiDiemKhongLapBindingSource.Filter = filter
        datasrrc.Tables(4).DefaultView.RowFilter = ""

    End Sub


    Sub ClearBindingCheckBox()
        cbxThuHai.DataBindings.Clear()
        cbxThuBa.DataBindings.Clear()
        cbxThuTu.DataBindings.Clear()
        cbxThuNam.DataBindings.Clear()
        cbxThuSau.DataBindings.Clear()
        cbxThuBay.DataBindings.Clear()
        cbxChuNhat.DataBindings.Clear()
    End Sub

    Sub RefreshBindingCheckBox()
        ClearBindingCheckBox()
        cbxThuHai.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuHai")
        cbxThuBa.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuBa")
        cbxThuTu.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuTu")
        cbxThuNam.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuNam")
        cbxThuSau.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuSau")
        cbxThuBay.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuBay")
        cbxChuNhat.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ChuNhat")
    End Sub
    Public Sub LoadData()

        datasrrc = New DataSet
        datasrrc.Tables.Add(TuyenBus.LayDanhSachTuyen())
        datasrrc.Tables.Add(LichTuyenBus.LayDanhSachLichTuyen())
        datasrrc.Tables.Add(LichTuyenBus.LayDanhSachLichTuyenLapThu())
        datasrrc.Tables.Add(LichTuyenBus.LayDanhSachLichTuyenKhongLap())
        datasrrc.Tables.Add(LichTuyenBus.LayDanhSachLichTuyenKhongLapThoiDiem())

        datasrrc.Tables(0).TableName = "Tuyen"
        datasrrc.Tables(1).TableName = "LichLap"
        datasrrc.Tables(2).TableName = "ThuLap"
        datasrrc.Tables(3).TableName = "TuyenKhongLap"
        datasrrc.Tables(4).TableName = "ThoiDiemKhongLap"

        tuyenBindingSource.DataSource = datasrrc
        tuyenBindingSource.DataMember = "Tuyen"
        'Tuyến lập

        datasrrc.Relations.Add("TuyenLichLap", datasrrc.Tables(0).Columns("MaTuyen"), datasrrc.Tables(1).Columns("MaTuyen"))
        datasrrc.Relations.Add("TuyenThuLap", datasrrc.Tables(0).Columns("MaTuyen"), datasrrc.Tables(2).Columns("MaTuyen"))

        tuyenLichLapBindingSource.DataSource = tuyenBindingSource
        tuyenLichLapBindingSource.DataMember = "TuyenLichLap"

        tuyenThuLapBindingSource.DataSource = tuyenBindingSource
        tuyenThuLapBindingSource.DataMember = "TuyenThuLap"

        'Tuyến không lập
        datasrrc.Relations.Add("TuyenLichKhongLap", datasrrc.Tables(0).Columns("MaTuyen"), datasrrc.Tables(3).Columns("MaTuyen"))

        tuyenNgayKhongLapBindingSource.DataSource = tuyenBindingSource
        tuyenNgayKhongLapBindingSource.DataMember = "TuyenLichKhongLap"

        tuyenThoiDiemKhongLapBindingSource.DataSource = datasrrc
        tuyenThoiDiemKhongLapBindingSource.DataMember = "ThoiDiemKhongLap"

    End Sub
    Public Sub BindingData()

        cbxTenTuyen.DataSource = tuyenBindingSource
        cbxTenTuyen.DisplayMember = "TenTuyen"
        cbxTenTuyen.ValueMember = "MaTuyen"

        dgvDanhSachLichLap.DataSource = tuyenLichLapBindingSource
        dgvNgayKhoiHanh.DataSource = tuyenNgayKhongLapBindingSource
        dgvDanhSachLichKhongLap.DataSource = tuyenThoiDiemKhongLapBindingSource

        cbxThuHai.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuHai")
        cbxThuBa.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuBa")
        cbxThuTu.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuTu")
        cbxThuNam.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuNam")
        cbxThuSau.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuSau")
        cbxThuBay.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ThuBay")
        cbxChuNhat.DataBindings.Add("Checked", tuyenThuLapBindingSource, "ChuNhat")

    End Sub
    Public Function xl_kiemtraThem() As Boolean
        Dim temp As String = txtGio.Text & ":" & txtPhut.Text

        If txtGio.Text = "" Or Not IsNumeric(txtGio.Text) Then
            txtGio.Focus()
            MessageBox.Show("Giờ không hợp lệ!", "Thông báo")
            Return False
        ElseIf CType(txtGio.Text, Integer) < 0 Or CType(txtGio.Text, Integer) > 24 Then
            txtGio.Focus()
            MessageBox.Show("24 Giờ không hợp lệ!", "Thông báo")
            Return False
        ElseIf txtPhut.Text = "" Or Not IsNumeric(txtPhut.Text) Then
            txtPhut.Focus()
            MessageBox.Show("Phút Không hợp lệ !", "Thông báo")
            Return False
        ElseIf CType(txtPhut.Text, Integer) < 0 Or CType(txtPhut.Text, Integer) > 60 Then
            txtPhut.Focus()
            MessageBox.Show("60 phút không hợp lệ!", "Thông báo")
            Return False
        Else
            For i As Integer = 0 To dgvDanhSachLichLap.Rows.Count - 1
                If temp = dgvDanhSachLichLap.Rows(i).Cells(2).Value Then
                    MessageBox.Show("Thời điểm hợp lệ !", "Thông báo")
                    Return False
                End If
            Next
        End If
        Return True
    End Function
    Public Function xl_kiemtraNgayKhoiHanh() As Boolean

        Dim ngayKhoiHanh As Date = dtpNgayKhoiHanh.Value
        For i As Integer = 0 To dgvNgayKhoiHanh.Rows.Count - 1
            If ngayKhoiHanh = CType(dgvNgayKhoiHanh.Rows(i).Cells(1).Value, Date) Then
                MessageBox.Show("Ngày khởi hành không lệ !", "Thông báo")
                Return False
            End If
        Next

        Return True
    End Function
    Public Function xl_kiemtraThemKhongLap() As Boolean
        Dim temp As String = txtGioKhongLap.Text & ":" & txtPhutKhongLap.Text

        If txtGioKhongLap.Text = "" Or Not IsNumeric(txtGioKhongLap.Text) Then
            txtGioKhongLap.Focus()
            MessageBox.Show("Giờ không hợp lệ!", "Thông báo")
            Return False
        ElseIf CType(txtGioKhongLap.Text, Integer) < 0 Or CType(txtGioKhongLap.Text, Integer) > 24 Then
            txtGioKhongLap.Focus()
            MessageBox.Show("24 Giờ không hợp lệ!", "Thông báo")
            Return False
        ElseIf txtPhutKhongLap.Text = "" Or Not IsNumeric(txtPhutKhongLap.Text) Then
            txtPhutKhongLap.Focus()
            MessageBox.Show("Phút Không hợp lệ !", "Thông báo")
            Return False
        ElseIf CType(txtPhutKhongLap.Text, Integer) < 0 Or CType(txtPhutKhongLap.Text, Integer) > 60 Then
            txtPhutKhongLap.Focus()
            MessageBox.Show("60 phút không hợp lệ!", "Thông báo")
            Return False
        Else
            For i As Integer = 0 To dgvDanhSachLichKhongLap.Rows.Count - 1
                If temp = dgvDanhSachLichKhongLap.Rows(i).Cells(3).Value Then
                    MessageBox.Show("Thời điểm hợp lệ !", "Thông báo")
                    Return False
                End If
            Next
            Return True
        End If
        Return True
    End Function
    Private Sub EndEdit()
        tuyenLichLapBindingSource.EndEdit()
        tuyenThuLapBindingSource.EndEdit()
        tuyenNgayKhongLapBindingSource.EndEdit()
        tuyenThoiDiemKhongLapBindingSource.EndEdit()
    End Sub
    Private Sub CancelEdit()
        tuyenLichLapBindingSource.CancelEdit()
        tuyenThuLapBindingSource.CancelEdit()
        tuyenNgayKhongLapBindingSource.CancelEdit()
        tuyenThoiDiemKhongLapBindingSource.CancelEdit()
    End Sub

    Private Sub btnHuyThoiDiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuyThoiDiem.Click
        tuyenThoiDiemKhongLapBindingSource.CancelEdit()
        tuyenNgayKhongLapBindingSource.CancelEdit()
    End Sub

    Private Sub dgvDanhSachLichKhongLap_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDanhSachLichKhongLap.CellContentClick
        If (dgvDanhSachLichKhongLap.Columns(e.ColumnIndex).Name = "colXoaThoiDiem") Then
            tuyenThoiDiemKhongLapBindingSource.RemoveAt(e.RowIndex)
        End If
    End Sub

End Class
